<!doctype html>
<html>
<head>
    <script src="../../../build/output/playcanvas-latest.js"></script>
    <link href="../../style.css" rel="stylesheet" />
</head>

<body>
    <!-- The canvas element -->
    <canvas id="application-canvas"></canvas>

    <script>
        function createMaterial(colors) {
            var material = new pc.PhongMaterial();
            for (var param in colors) {
                material[param] = colors[param];
            }
            material.update();
            return material;
        }
    </script>

    <!-- The script -->
    <script>
        var canvas = document.getElementById("application-canvas");

        // Create the application and start the update loop
        var app = new pc.Application(canvas);
        app.start();

        // Set the canvas to fill the window and automatically change resolution to be the same as the canvas size
        app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
        app.setCanvasResolution(pc.RESOLUTION_AUTO);

        var entity, light, camera;

        // Create an Entity with a camera component
        var camera = new pc.Entity();
        camera.addComponent("camera", {
            clearColor: new pc.Color(0,0,0)
        });

        camera.rotateLocal(-30, 0, 0);
        camera.translateLocal(0, 0, 5);

        // Create an Entity for the ground
        var ground = new pc.Entity();
        ground.addComponent("model", {
            type: "box"
        });
        ground.setLocalScale(50, 1, 50);
        ground.setLocalPosition(0, -0.5, 0);

        // Create an Entity with a point light component and a sphere model component.
        var lightDir = new pc.Entity();
        lightDir.addComponent("light", {
            type: "directional",
            color: new pc.Color(1, .4, 0),
            intensity: 1
        });
        lightDir.setLocalEulerAngles(-80, 70, 0);

        var light = new pc.Entity();
        light.addComponent("light", {
            type: "spot",
            color: new pc.Color(.6, .85, 1),
            outerConeAngle: 45,
            innerConeAngle: 40,
            range: 20,
            intensity: 2,
            castShadows: true,
            shadowBias: 0.005,
            shadowResolution: 2048
        });
        light.setLocalPosition(0, 5, 0);
        light.addComponent("audiolistener");

        var requests = [{
            url: "../../assets/footsteps_gravel_01.mp3",
            type: "audio"
        }, {
            url: "../../assets/Playbot/Playbot.json",
            type: "model"
        }, {
            url: "../../assets/Playbot/Playbot_run.json",
            type: "animation"
        }];

        var count = requests.length;
        var assets = [];
        for (var i = 0; i < requests.length; i++) {
            app.assets.loadFromUrl(requests[i].url, requests[i].type, function (err, asset) {
                count--;
                if (count === 0) {
                    var gravel = app.assets.find("footsteps_gravel_01.mp3");
                    var model = app.assets.find("Playbot.json");
                    var anim = app.assets.find("Playbot_run.json");

                    // Create walking dude
                    entity = new pc.Entity();

                    // add sound component
                    entity.addComponent('sound');

                    // add footsteps slot
                    entity.sound.addSlot('footsteps', {
                        asset: gravel,
                        pitch: 1.7,
                        loop: true,
                        autoPlay: true
                    });

                    // add model
                    entity.addComponent("model", {
                        type: "asset",
                        asset: model,
                        castShadows: true
                    });

                    // add animation
                    entity.addComponent("animation", {
                        assets: [anim],
                        speed: 0.8
                    });

                    // add entity in the hierarchy
                    app.root.addChild(entity);

                    var pos;
                    var angle = 135;
                    var radius = 3;
                    var height = 0;//1.1;
                    app.on("update", function (dt) {
                        angle += 30*dt;
                        if (angle > 360) {
                            angle -= 360;
                        }
                        var pos = camera.getPosition();
                        entity.setLocalPosition(radius * Math.sin(angle*pc.math.DEG_TO_RAD), height, radius * Math.cos(angle*pc.math.DEG_TO_RAD));
                        entity.setLocalEulerAngles(0, angle+90, 0);
                    });
                }
            });
        };

        // Add Entities into the scene hierarchy
        app.root.addChild(camera);
        app.root.addChild(light);
        app.root.addChild(ground);
    </script>
</body>
</html>
